﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Packager
{
    internal class Builder
    {
        private static string visualStudioFolder = @"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\";
        private static string VisualStudioDevenvCom { get { return visualStudioFolder + "devenv.com"; } }

        public static void Build(string solution, string configuration)
        {
            string comandParam = "\"" + System.IO.Path.Combine(Properties.Settings.Default.ProjectFolder, solution) + "\"" + string.Format(@" /{0} {1}", "Rebuild", configuration);
            if (!System.IO.File.Exists(System.IO.Path.Combine(Properties.Settings.Default.ProjectFolder, solution)))
            {
                LogManagement.LogMessage("File not found: " + solution);
                return;
            }

            ProcessRequest(new ProcessStartInfo(VisualStudioDevenvCom, comandParam));
        }


        //------------------------------------------------------------------------------------------------
        public static string GetFrameworkVersion()
        {
            return System.Reflection.Assembly.GetExecutingAssembly().ImageRuntimeVersion;
        }

        //------------------------------------------------------------------------------------------------
        public static void ProcessRequest(ProcessStartInfo info)
        {
            info.UseShellExecute = false;
            info.RedirectStandardInput = true;
            info.RedirectStandardOutput = true;
            info.RedirectStandardError = true;
            info.CreateNoWindow = true;

            using (Process proc = Process.Start(info))
            {
                proc.StandardInput.AutoFlush = true;
                proc.BeginOutputReadLine();
                proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);
                proc.ErrorDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);
                proc.WaitForExit();

                proc.OutputDataReceived -= new DataReceivedEventHandler(proc_OutputDataReceived);
                proc.ErrorDataReceived -= new DataReceivedEventHandler(proc_OutputDataReceived);
            }
        }

        //------------------------------------------------------------------------------------------------
        static void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            LogManagement.LogMessage(e.Data);
        }
    }

    internal class LogManagement
    {
        //------------------------------------------------------------------------------------------------
        public static event EventHandler<LogToOutputEventArgs> LogToOutput;

        //------------------------------------------------------------------------------------------------
        public static void LogMessage(string toLog)
        {
            if (LogToOutput != null)
                LogToOutput(null, new LogToOutputEventArgs(toLog));
        }
    }

    //====================================================================================================
    internal class LogToOutputEventArgs : EventArgs
    {
        private string output;

        //------------------------------------------------------------------------------------------------
        public LogToOutputEventArgs(string output)
        {
            this.output = output;
        }

        //------------------------------------------------------------------------------------------------
        public string Output { get { return output; } set { output = value; } }
    }
}
